package com.hbnu.ojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.util.List;

/**
 * @Description: Docker容器测试
 * @Author: Hu Bowen
 */
public class DockerDemo {

    public static void main(String[] args) throws InterruptedException {




        // 获取默认的Docker Client
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
        // PingCmd pingCmd = dockerClient.pingCmd();
        // pingCmd.exec();
        // 拉取镜像
        String image = "nginx:latest";

        // // 下载镜像
        // PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image);
        // // 拉取镜像结果回调
        // PullImageResultCallback pullImageResultCallback = new PullImageResultCallback() {
        //     // 异步获取
        //     @Override
        //     public void onNext(PullResponseItem item) {
        //         // 获取状态信息
        //         System.out.println("下载镜像:" + item.getStatus());
        //         super.onNext(item);
        //     }
        // };
        // pullImageCmd
        //         .exec(pullImageResultCallback)
        //         .awaitCompletion();
        // System.out.println("下载完成");

        // 1. 创建容器
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
        CreateContainerResponse createContainerResponse = containerCmd
                .withCmd("echo", "Hello Docker")
                .exec();
        System.out.println(createContainerResponse);
        String containerId = createContainerResponse.getId();

        // 2. 查看容器状态
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        // 展示所有容器
        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
        for (Container container : containerList) {
            // 遍历容器列表
            System.out.println(container);
        }

        // 启动容器
        dockerClient.startContainerCmd(containerId).exec();

        // Thread.sleep(5000L); // 睡眠10s

        // 4. 查看日志
        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                System.out.println(item.getStreamType());
                System.out.println("日志" + new String(item.getPayload()));
                super.onNext(item);
            }
        };

        dockerClient.logContainerCmd(containerId)
                .withStdErr(true)
                .withStdOut(true)
                .exec(logContainerResultCallback)
                .awaitCompletion();  // 等待执行完

        // 5. 删除容器
        dockerClient.removeContainerCmd(containerId).withForce(true).exec();

        // 删除镜像
        dockerClient.removeImageCmd(image).exec();

    }
}
